import pygame
import random
from src.base_game import BaseGame


class PongGame(BaseGame):
    """Simple Pong game implementation"""
    
    def __init__(self, screen: pygame.Surface, resource_manager):
        super().__init__("Pong", screen, resource_manager)
        
    def initialize(self) -> None:
        """Initialize Pong game"""
        self.screen_width = self.screen.get_width()
        self.screen_height = self.screen.get_height()
        
        # Paddle settings
        self.paddle_width = 10
        self.paddle_height = 100
        self.paddle_speed = 300
        
        # Ball settings
        self.ball_size = 20
        self.ball_speed = 250
        
        # Player paddle
        self.player_paddle = pygame.Rect(
            50, self.screen_height // 2 - self.paddle_height // 2,
            self.paddle_width, self.paddle_height
        )
        
        # AI paddle
        self.ai_paddle = pygame.Rect(
            self.screen_width - 50 - self.paddle_width,
            self.screen_height // 2 - self.paddle_height // 2,
            self.paddle_width, self.paddle_height
        )
        
        # Ball
        self.ball = pygame.Rect(
            self.screen_width // 2 - self.ball_size // 2,
            self.screen_height // 2 - self.ball_size // 2,
            self.ball_size, self.ball_size
        )
        
        # Ball velocity
        self.ball_vel_x = self.ball_speed * random.choice([-1, 1])
        self.ball_vel_y = self.ball_speed * random.choice([-1, 1])
        
        # Scores
        self.player_score = 0
        self.ai_score = 0
        
        # Colors
        self.white = (255, 255, 255)
        self.black = (0, 0, 0)
        
        # Font
        self.font = pygame.font.Font(None, 74)
        
    def handle_event(self, event: pygame.event.Event) -> None:
        """Handle Pong-specific events"""
        pass
        
    def update(self, dt: float) -> None:
        """Update Pong game logic"""
        keys = pygame.key.get_pressed()
        
        # Player paddle movement
        if keys[pygame.K_w] and self.player_paddle.top > 0:
            self.player_paddle.y -= self.paddle_speed * dt
        if keys[pygame.K_s] and self.player_paddle.bottom < self.screen_height:
            self.player_paddle.y += self.paddle_speed * dt
            
        # AI paddle movement (simple AI)
        if self.ai_paddle.centery < self.ball.centery:
            self.ai_paddle.y += self.paddle_speed * dt * 0.7
        elif self.ai_paddle.centery > self.ball.centery:
            self.ai_paddle.y -= self.paddle_speed * dt * 0.7
            
        # Keep AI paddle on screen
        if self.ai_paddle.top < 0:
            self.ai_paddle.top = 0
        if self.ai_paddle.bottom > self.screen_height:
            self.ai_paddle.bottom = self.screen_height
            
        # Ball movement
        self.ball.x += self.ball_vel_x * dt
        self.ball.y += self.ball_vel_y * dt
        
        # Ball collision with top and bottom walls
        if self.ball.top <= 0 or self.ball.bottom >= self.screen_height:
            self.ball_vel_y = -self.ball_vel_y
            
        # Ball collision with paddles
        if self.ball.colliderect(self.player_paddle) or self.ball.colliderect(self.ai_paddle):
            self.ball_vel_x = -self.ball_vel_x
            
        # Ball goes off screen (scoring)
        if self.ball.left <= 0:
            self.ai_score += 1
            self.reset_ball()
        elif self.ball.right >= self.screen_width:
            self.player_score += 1
            self.reset_ball()
            
    def reset_ball(self) -> None:
        """Reset ball to center with random direction"""
        self.ball.center = (self.screen_width // 2, self.screen_height // 2)
        self.ball_vel_x = self.ball_speed * random.choice([-1, 1])
        self.ball_vel_y = self.ball_speed * random.choice([-1, 1])
        
    def render(self) -> None:
        """Render Pong game"""
        # Clear screen
        self.screen.fill(self.black)
        
        # Draw paddles
        pygame.draw.rect(self.screen, self.white, self.player_paddle)
        pygame.draw.rect(self.screen, self.white, self.ai_paddle)
        
        # Draw ball
        pygame.draw.rect(self.screen, self.white, self.ball)
        
        # Draw center line
        for i in range(0, self.screen_height, 40):
            pygame.draw.rect(self.screen, self.white, 
                           (self.screen_width // 2 - 2, i, 4, 20))
            
        # Draw scores
        player_text = self.font.render(str(self.player_score), True, self.white)
        ai_text = self.font.render(str(self.ai_score), True, self.white)
        
        self.screen.blit(player_text, (self.screen_width // 4, 50))
        self.screen.blit(ai_text, (3 * self.screen_width // 4, 50))
        
        # Draw instructions
        instruction_font = pygame.font.Font(None, 36)
        instruction_text = instruction_font.render("W/S to move, ESC to quit", True, self.white)
        self.screen.blit(instruction_text, (10, self.screen_height - 40))